﻿using LeaderCCSLaserUI.Data;
using LeaderCCSLaserUI.Models;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DialogResult = Prism.Services.Dialogs.DialogResult;
using MessageBox = System.Windows.Forms.MessageBox;

namespace LeaderCCSLaserUI.ViewModels.Dialogs.VisionProgram
{
    public class AddVisionFunctionDialogViewModel : DialogViewModel
    {
        #region 变量
        private string partNum = string.Empty;
        #endregion
        #region 属性
        private ObservableCollection<VisionFunctionDisp> functionList = new ObservableCollection<VisionFunctionDisp>();
        public ObservableCollection<VisionFunctionDisp> FunctionList
        {
            get { return functionList; }
            set { SetProperty(ref functionList, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> selectButtonCommand;
        public DelegateCommand<object> SelectButtonCommand =>
            selectButtonCommand ?? (selectButtonCommand = new DelegateCommand<object>(ExecuteSelectButtonCommand));

        void ExecuteSelectButtonCommand(object obj)
        {
            if (int.TryParse(obj.ToString(),out var id))
            {
                DialogParameters param = new DialogParameters();
                param.Add("FuntionId", id);
                OnRequestClose(new DialogResult(ButtonResult.Yes, param));
            }
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            partNum = parameters.GetValue<string>("PartNum");
            LoadFunction(partNum);


        }
        #endregion
        #region 构造函数
        public AddVisionFunctionDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            Title = "选择方法";
        }
        #endregion
        #region 功能函数
        private void LoadFunction(string partNum)
        {
            FunctionList.Clear();
            try
            {
                using var mdb = new MDbContext(partNum);
                var programs = mdb.VisionFunctions.Where(p => p.Id > 0);
                if (programs.Any())
                {
                    programs.ToList().ForEach(p =>
                    {
                        FunctionList.Add(new VisionFunctionDisp
                        {
                            Id = p.Id,
                            Name = p.Name,
                            Alias = p.Alias,
                            IconPath = Path.Join(System.AppDomain.CurrentDomain.BaseDirectory, p.IconPath) 
                        });
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
    }
}
